<template>
  <div class="app-container">
    <router-view v-if="!loading" />
    <div v-else class="loading-container">
      <div class="loading-content">
        <div class="el-icon-loading"></div>
        <span>加载中...</span>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, computed, inject, watch } from 'vue'
import router from './router'

export default {
  name: 'App',
  setup() {
    const userStore = inject('userStore')
    const loading = computed(() => userStore.loading.value)
    
    onMounted(() => {
      // 初始化时检查用户认证状态
      const isAuthenticated = userStore.checkAuth()
      // 如果未认证且当前不是登录页，跳转到登录页
      if (!isAuthenticated && router.currentRoute.value.path !== '/login') {
        router.push('/login')
      }
    })
    
    // 监听loading状态变化，确保认证完成后如果未登录则跳转
    watch(loading, (newVal) => {
      // 当loading完成且不是登录页时检查认证状态
      if (!newVal && !userStore.isLoggedIn.value && router.currentRoute.value.path !== '/login') {
        router.push('/login')
      }
    })
    
    return {
      loading
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.app-container {
  min-height: 100vh;
}

.loading-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }
  
  .loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }
  
  .loading-content .el-icon-loading {
    width: 32px;
    height: 32px;
    animation: rotating 1s linear infinite;
    color: #409eff;
    font-size: 32px;
  }
  
  @keyframes rotating {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
</style>